package com.config;

/**
 * @auther: zhangxin
 * @Description:  代替spring.xml
 */

import com.github.pagehelper.PageInterceptor;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.*;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.Properties;


@Configuration
@ComponentScan(value="com",
        excludeFilters = {
                @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class})
        }
)
// 开启AOP
@EnableAspectJAutoProxy
// 开启事务支持
@EnableTransactionManagement
// 引入数据源配置
@Import({DataSourceConfiguration.class})
public class SpringConfiguration {

        // MyBatis集成
        @Bean
        public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) {
                SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
                // 设置数据源
                sqlSessionFactoryBean.setDataSource(dataSource);
                // 设置别名包
                sqlSessionFactoryBean.setTypeAliasesPackage("com.entity,com.vo");
                // 设置分页插件
                PageInterceptor pageInterceptor = new PageInterceptor();
                Properties properties = new Properties();
                properties.put("helperDialect", "mysql");
                pageInterceptor.setProperties(properties);
                sqlSessionFactoryBean.setPlugins(pageInterceptor);
                return sqlSessionFactoryBean;
        }

        @Bean
        public MapperScannerConfigurer mapperScannerConfigurer() throws Exception {
                MapperScannerConfigurer configurer = new MapperScannerConfigurer();
                // 设置SessonFactory名字
                configurer.setSqlSessionFactoryBeanName("sqlSessionFactoryBean");
                // 设置扫描包
                configurer.setBasePackage("com.mapper");
                return configurer;
        }

        // 用于创建事务管理器对象
        @Bean(name = "transactionManager")
        public PlatformTransactionManager transactionManager(DataSource dataSource) {
                return new DataSourceTransactionManager(dataSource);
        }
}
